package com.lovo.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.bean.User;
import com.lovo.dao.UserMapper;
import com.lovo.service.UserService;
import com.lovo.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    @Override
    public PageInfo<User> getUsers(int pageNo , int limit) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        PageHelper.startPage(pageNo, limit);
        List<User> list = mapper.selectAll();
        PageInfo<User> pageInfo = new PageInfo<>(list);
        SqlSessionUtil.commit();
        return pageInfo;
    }

    @Override
    public boolean addUser(User user) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.addUser(user);
        SqlSessionUtil.commit();
        return i>0;
    }

    @Override
    public boolean updateUser(User user) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.updateUser(user);
        SqlSessionUtil.commit();
        return i>0;
    }

    @Override
    public boolean deleteUser(int parseInt) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.deleteUser(parseInt);
        SqlSessionUtil.commit();
        return i>0;
    }

    @Override
    public PageInfo<User> queryUser(String type, String content, int parseInt, int parseInt1) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        PageHelper.startPage(parseInt,parseInt1);
        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        map.put("content",content);
        List<User> list = mapper.selectByquery(map);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        SqlSessionUtil.commit();
        return pageInfo;
    }

    @Override
    public User login(String userName, String userPwd) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Map<String,Object> map = new HashMap<>();
        map.put("userName",userName);
        map.put("userPwd",userPwd);
        User user = mapper.login(map);
        SqlSessionUtil.commit();
        return user;
    }

    @Override
    public User getUserById(int userId) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectByPrimaryId(userId);
        SqlSessionUtil.commit();
        return user;
    }
}
